package org.gp953.order.controller;

import org.gp953.basic.exception.BusinessException;
import org.gp953.basic.util.AjaxResult;
import org.gp953.basic.util.LoginContext;
import org.gp953.basic.util.PageList;
import org.gp953.order.domain.ProductOrder;
import org.gp953.order.query.ProductOrderQuery;
import org.gp953.order.service.IProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/productOrder")
public class ProductOrderController {

    @Autowired
    private IProductOrderService productOrderService;

    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String, Object> params, HttpServletRequest request) {

        try {
            String payData = productOrderService.submit(params, LoginContext.getLoginInfo(request));
            System.out.println(payData);
            return AjaxResult.me().setResultObj(payData);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下单失败！" + e.getMessage());
        }


    }

    //平台员工
    @PostMapping("/admin")
    public PageList<ProductOrder> admin(@RequestBody ProductOrderQuery query, HttpServletRequest request){
        return productOrderService.admin(query,LoginContext.getLoginInfo(request));
    }

    //用户
    @PostMapping("/user")
    public PageList<ProductOrder> user(@RequestBody ProductOrderQuery query, HttpServletRequest request){
        return productOrderService.user(query,LoginContext.getLoginInfo(request));
    }

    /**
     * 查询单条数据详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ProductOrder getInfo(@PathVariable("id") Long id){
        ProductOrder productOrder = productOrderService.queryById(id);
        System.out.println(productOrder);
        return productOrder;
    }

    @PutMapping("/{id}")
    public AjaxResult cancelOrder(@PathVariable("id") Long id){
        try {
            productOrderService.cancelOrder(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

}
